{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1bee72a5-e25c-45f6-b6e2-b08adb65a992",
   "metadata": {
    "tags": [
     "remove_cell"
    ]
   },
   "outputs": [],
   "source": [
    "import datapane as dp\n",
    "import warnings\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "import matplotlib.pyplot as plt\n",
    "plt.ioff() "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37c2a3bd-e9e4-4b86-b06c-37c0478e6403",
   "metadata": {},
   "source": [
    "# Plots\n",
    "\n",
    "::: datapane.Plot"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b17b2027-ae6a-44aa-8be3-8549e560800b",
   "metadata": {},
   "source": [
    "Datapane currently supports the following libraries:\n",
    "\n",
    "| Library | Site / Docs |\n",
    "| :--- | :--- |\n",
    "| [Altair](#altair) | [https://altair-viz.github.io/](https://altair-viz.github.io/) |\n",
    "| [Matplotlib / Seaborn](#matplotlib) | [https://matplotlib.org/](https://matplotlib.org/) / [https://seaborn.pydata.org/](https://seaborn.pydata.org/) |\n",
    "| [Bokeh](#bokeh) | [https://bokeh.org/](https://bokeh.org/) |\n",
    "| [Plotly](#plotly) | [https://plotly.com/python/](https://plotly.com/python/) |\n",
    "| [Folium](#folium) | [https://python-visualization.github.io/folium/](https://python-visualization.github.io/folium/) |\n",
    "\n",
    "\n",
    "If you're using another visualization library e.g. Pyvis for networks, try saving your chart as a local HTML file and wrapping that in a [dp.HTML](/reference/blocks/text) block. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6dba1d29-43db-420f-a3ac-a0b345620c60",
   "metadata": {},
   "source": [
    "## Altair\n",
    "\n",
    "[Altair](https://altair-viz.github.io/) is a declarative statistical visualization library for Python, based on [Vega](http://vega.github.io/vega) and [Vega-Lite](http://vega.github.io/vega-lite). Altair’s API is simple, friendly and consistent and built on top of the powerful [Vega-Lite](http://vega.github.io/vega-lite) visualization grammar. This elegant simplicity produces beautiful and effective visualizations with a minimal amount of code.\n",
    "\n",
    "To get started using Altair to make your visualizations, begin with Altair's [Documentation](https://altair-viz.github.io/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "918f4d84-1086-47ac-94ba-e32f04f63c42",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import altair as alt\n",
    "import pandas as pd\n",
    "from vega_datasets import data as vega_data\n",
    "\n",
    "gap = pd.read_json(vega_data.gapminder.url)\n",
    "\n",
    "select_year = alt.selection_point(\n",
    "    name=\"select\",\n",
    "    fields=[\"year\"],\n",
    "    value={\"year\": 1955},\n",
    "    bind=alt.binding_range(min=1955, max=2005, step=5),\n",
    ")\n",
    "\n",
    "alt_chart = (\n",
    "    alt.Chart(gap)\n",
    "    .mark_point(filled=True)\n",
    "    .encode(\n",
    "        alt.X(\"fertility\", scale=alt.Scale(zero=False)),\n",
    "        alt.Y(\"life_expect\", scale=alt.Scale(zero=False)),\n",
    "        alt.Size(\"pop:Q\"),\n",
    "        alt.Color(\"cluster:N\"),\n",
    "        alt.Order(\"pop:Q\", sort=\"descending\"),\n",
    "    )\n",
    "    .add_params(select_year)\n",
    "    .transform_filter(select_year)\n",
    ")\n",
    "\n",
    "dp.Plot(alt_chart)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a38f249d-8567-4db6-953c-8ea8ae57dc79",
   "metadata": {},
   "source": [
    "## Bokeh\n",
    "\n",
    "Bokeh is an interactive visualization library which provides elegant, concise construction of versatile graphics, and affords high-performance interactivity over large datasets. \n",
    "\n",
    "To get started using Bokeh to make your visualizations, begin with Bokeh's [User Guide](https://docs.bokeh.org/en/latest/docs/user_guide.html#userguide)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "676fc6ae-9b8b-4be4-aa66-601478dee429",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from bokeh.plotting import figure, output_file, show\n",
    "from bokeh.sampledata.iris import flowers\n",
    "\n",
    "colormap = {\"setosa\": \"red\", \"versicolor\": \"green\", \"virginica\": \"blue\"}\n",
    "colors = [colormap[x] for x in flowers[\"species\"]]\n",
    "\n",
    "bokeh_chart = figure(title=\"Iris Morphology\")\n",
    "bokeh_chart.xaxis.axis_label = \"Petal Length\"\n",
    "bokeh_chart.yaxis.axis_label = \"Petal Width\"\n",
    "\n",
    "bokeh_chart.circle(\n",
    "    flowers[\"petal_length\"],\n",
    "    flowers[\"petal_width\"],\n",
    "    color=colors,\n",
    "    fill_alpha=0.2,\n",
    "    size=10,\n",
    ")\n",
    "\n",
    "dp.Plot(bokeh_chart)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bd40ed1-212a-412c-ab58-ec637edaefea",
   "metadata": {},
   "source": [
    "## Matplotlib\n",
    "\n",
    "[Matplotlib](https://matplotlib.org) is the original Python visualization library, often supported and used with [Jupyter Notebooks](https://jupyter.org/). Matplotlib plots are not interactive in Datapane apps, but are saved as SVGs so can be viewed at high fidelity.\n",
    "\n",
    "Higher-level matplotlib libraries such as [Seaborn](https://seaborn.pydata.org/) are also supported, and can be used in a similar way to the matplotlib example below,"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fbd100cd-2fdd-4a70-a4a0-cab3432e58c0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd\n",
    "from vega_datasets import data as vega_data\n",
    "\n",
    "gap = pd.read_json(vega_data.gapminder.url)\n",
    "fig = gap.plot.scatter(x=\"life_expect\", y=\"fertility\")\n",
    "\n",
    "\n",
    "dp.Plot(fig)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "407afe18-00a8-418c-9748-9e3279c897fd",
   "metadata": {},
   "source": [
    "!!! info\n",
    "    \n",
    "    You can pass either a `matplotlib` `Figure` or `Axes` object to `dp.Plot`,  you can obtain the current global figure from `matplotlib` by running `plt.gcf()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e5529b5-5e89-48e3-b87e-48a83f0b8146",
   "metadata": {},
   "source": [
    "## Plotly\n",
    "\n",
    "[Plotly's Python graphing library](https://plotly.com/python/) makes interactive, publication-quality graphs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f9d173e-7452-4ef1-9092-6a98f0588c30",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import plotly.express as px\n",
    "\n",
    "df = px.data.gapminder()\n",
    "\n",
    "plotly_chart = px.scatter(\n",
    "    df.query(\"year==2007\"),\n",
    "    x=\"gdpPercap\",\n",
    "    y=\"lifeExp\",\n",
    "    size=\"pop\",\n",
    "    color=\"continent\",\n",
    "    hover_name=\"country\",\n",
    "    log_x=True,\n",
    "    size_max=60,\n",
    ")\n",
    "\n",
    "dp.Plot(plotly_chart)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "4e6971f1-ca78-4afc-807f-5372f0d0af51",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Folium\n",
    "\n",
    "[Folium](https://python-visualization.github.io/folium/) makes it easy to visualize data that’s been manipulated in Python on an interactive leaflet map. It enables both the binding of data to a map for `choropleth` visualizations as well as passing rich vector/raster/HTML visualizations as markers on the map.\n",
    "\n",
    "The library has a number of built-in tilesets from OpenStreetMap, Mapbox, and Stamen, and supports custom tilesets with Mapbox or Cloudmade API keys. \n",
    "\n",
    "!!! info\n",
    "    \n",
    "    If your folium map consumes live data which expires after a certain time, you can automate it to refresh the map on a cadence. See [dp.Dynamic](/reference/apps/compute-blocks/dynamic/)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1a28dd4-6dc5-42c4-89c9-43d3afc16315",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import folium\n",
    "\n",
    "m = folium.Map(location=[45.5236, -122.6750])\n",
    "\n",
    "dp.Plot(m)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
